# -*- coding: utf-8 -*-
# coding: utf-8
"""
处理文件上传的后端代码
"""

from flask import request
from flask_restful import Resource
import os
import uuid

# 获取当前文件的目录
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

# 定义相对路径，并转换为绝对路径
UPLOAD_FOLDER = os.path.join(BASE_DIR, '../Data/input')

# 确保目录存在
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)


class FileUpload(Resource):
    def post(self):
        if 'file' not in request.files:
            return {"message": "No file part"}, 400
        file = request.files['file']
        if file.filename == '':
            return {"message": "No selected file"}, 400

        # 生成唯一的文件名
        unique_filename = str(uuid.uuid4()) + os.path.splitext(file.filename)[1]
        file_path = os.path.join(UPLOAD_FOLDER, unique_filename)
        file.save(file_path)

        return {"message": "File uploaded successfully", "Authorization": 1125, "id": unique_filename}, 200
